package com.lxmes.hr.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lxmes.common.annotation.Log;
import com.lxmes.common.core.controller.BaseController;
import com.lxmes.common.core.domain.AjaxResult;
import com.lxmes.common.enums.BusinessType;
import com.lxmes.hr.domain.NcWagesGzhs;
import com.lxmes.hr.service.INcWagesGzhsService;
import com.lxmes.common.utils.poi.ExcelUtil;
import com.lxmes.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 工资核算Controller
 *
 * @author lx
 * @date 2023-03-21
 */
@RestController
@RequestMapping("/hr/gzhs")
public class NcWagesGzhsController extends BaseController
{
    @Autowired
    private INcWagesGzhsService ncWagesGzhsService;


    /**
     * 查询工资核算列表
     */
    @PreAuthorize("@ss.hasPermi('hr:gzhs:list')")
    @GetMapping("/list")
    public TableDataInfo list(NcWagesGzhs ncWagesGzhs)
    {
        startPage();
        List<NcWagesGzhs> list = ncWagesGzhsService.selectNcWagesGzhsList(ncWagesGzhs);
        return getDataTable(list);
    }

    /**
     * 导出工资核算列表
     */
    @PreAuthorize("@ss.hasPermi('hr:gzhs:export')")
    @Log(title = "工资核算", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NcWagesGzhs ncWagesGzhs)
    {
        List<NcWagesGzhs> list = ncWagesGzhsService.selectNcWagesGzhsList(ncWagesGzhs);
        ExcelUtil<NcWagesGzhs> util = new ExcelUtil<NcWagesGzhs>(NcWagesGzhs.class);
        util.exportExcel(response, list, "工资核算数据");
    }

    /**
     * 获取工资核算详细信息
     */
    @PreAuthorize("@ss.hasPermi('hr:gzhs:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(ncWagesGzhsService.selectNcWagesGzhsById(id));
    }

    /**
     * 新增工资核算
     */
    @PreAuthorize("@ss.hasPermi('hr:gzhs:add')")
    @Log(title = "工资核算", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NcWagesGzhs ncWagesGzhs)
    {
        return toAjax(ncWagesGzhsService.insertNcWagesGzhs(ncWagesGzhs));
    }

    /**
     * 修改工资核算
     */
    @PreAuthorize("@ss.hasPermi('hr:gzhs:edit')")
    @Log(title = "工资核算", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NcWagesGzhs ncWagesGzhs)
    {
        return toAjax(ncWagesGzhsService.updateNcWagesGzhs(ncWagesGzhs));
    }

    /**
     * 删除工资核算
     */
    @PreAuthorize("@ss.hasPermi('hr:gzhs:remove')")
    @Log(title = "工资核算", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ncWagesGzhsService.deleteNcWagesGzhsByIds(ids));
    }

    /**
     * 导入工资核算
     */
    @Log(title = "工资核算", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<NcWagesGzhs> util = new ExcelUtil<>(NcWagesGzhs.class);
        List<NcWagesGzhs> ncWagesGzhsList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = ncWagesGzhsService.imporIteminfo(ncWagesGzhsList, updateSupport, operName);
        return AjaxResult.success(message);
    }
    // 下载模板
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        ExcelUtil<NcWagesGzhs> util = new ExcelUtil<>(NcWagesGzhs.class);
        util.importTemplateExcel(response,"工资核算");
    }

    @PreAuthorize("@ss.hasPermi('hr:account:add')")
    @Log(title = "核算")
    @GetMapping("/accountWage/{ny}/{sjly}")
    public AjaxResult accountWage(@PathVariable("ny") String ny,@PathVariable("sjly") String sjly)
    {
        int i = ncWagesGzhsService.accountWage(ny,sjly);
        if(i==1){
            return toAjax(1);
        }else if(i==0){
            return AjaxResult.success("0");
        }else if(i==-1){
            return AjaxResult.success("-1");
        }else{
            return AjaxResult.error();
        }
    }
}
